package com.pig4cloud.pig.module.service.platform.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.module.api.entity.platform.PlatformHelp;
import com.pig4cloud.pig.module.api.vo.TreeSelect;
import com.pig4cloud.pig.module.mapper.platform.PlatformHelpMapper;
import com.pig4cloud.pig.module.service.platform.IPlatformHelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 帮助文档Service业务层处理
 *
 * @author ruoyi
 * @date 2021-10-30
 */
@Service
public class PlatformHelpServiceImpl extends ServiceImpl<PlatformHelpMapper, PlatformHelp> implements IPlatformHelpService {


	@Resource
	private PlatformHelpMapper platformHelpMapper;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Override
	public List<PlatformHelp> getNoPageByList(PlatformHelp eventType) {
		QueryWrapper<PlatformHelp> qw = new QueryWrapper<>();
		qw.orderByAsc("sort");
		List<PlatformHelp> list = platformHelpMapper.selectList(qw);
		return list;
	}

	@Override
	public List<TreeSelect> buildPlatformHelpTreeSelect(List<PlatformHelp> platformHelps) {
		List<TreeSelect> list = new ArrayList<>();
//        boolean blHasKey = redisTemplate.opsForHash().hasKey("eventTypeTree","eventTypeTree");
//        if (blHasKey) {
//            Object object = redisTemplate.opsForHash().get("eventTypeTree", "eventTypeTree");
//            String itemDicts = object != null ? object.toString() : "";
//            if (!TextUtils.isBlank(itemDicts)) {
//                JSONArray jsonArray = JSONArray.parseArray(itemDicts);
//                list = JSONArray.parseArray(jsonArray.toJSONString(), TreeSelect.class);
//            } else {
//                list = getEventTypeTreeList(eventTypeList);
//                redisTemplate.opsForHash().put("eventTypeTree", "eventTypeTree", JSONArray.toJSONString(list));
//            }
//        } else {
//            list = getEventTypeTreeList(eventTypeList);
//            redisTemplate.opsForHash().put("eventTypeTree", "eventTypeTree", JSONArray.toJSONString(list));
//        }
		list = getPlatformHelpTreeList(platformHelps);
//		redisTemplate.opsForHash().put("eventTypeTree", "eventTypeTree", JSONUtil.toJsonStr(list));
		return list;
	}

	private List<TreeSelect> getPlatformHelpTreeList(List<PlatformHelp> eventTypeList){
		List<PlatformHelp> eventTypes = buildPlatformHelpTree(eventTypeList);
		return eventTypes.stream().map(TreeSelect::new).collect(Collectors.toList());
	}

	/**
	 * 构建前端所需要树结构
	 *
	 * @param platformHelps 事件类型
	 * @return 树结构列表
	 */
	@Override
	public List<PlatformHelp> buildPlatformHelpTree(List<PlatformHelp> platformHelps) {
		List<PlatformHelp> returnList = new ArrayList<>();
		List<Integer> tempList = new ArrayList<>();
		for (PlatformHelp eventType : platformHelps) {
			tempList.add(eventType.getId());
		}
		for (Iterator<PlatformHelp> iterator = platformHelps.iterator(); iterator.hasNext();) {
			PlatformHelp platformHelp = iterator.next();
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(platformHelp.getParentId())) {
				recursionFn(platformHelps, platformHelp);
				returnList.add(platformHelp);
			}
		}
		if (returnList.isEmpty()) {
			returnList = platformHelps;
		}
		return returnList;
	}


	/**
	 * 递归列表
	 */
	private void recursionFn(List<PlatformHelp> list, PlatformHelp t)
	{
		// 得到子节点列表
		List<PlatformHelp> childList = getChildList(list, t);
		t.setChildren(childList);
		for (PlatformHelp tChild : childList) {
			if (hasChild(list, tChild)) {
				recursionFn(list, tChild);
			}
		}
	}
	/**
	 * 得到子节点列表
	 */
	private List<PlatformHelp> getChildList(List<PlatformHelp> list, PlatformHelp t)
	{
		List<PlatformHelp> tlist = new ArrayList<>();
		Iterator<PlatformHelp> it = list.iterator();
		while (it.hasNext()) {
			PlatformHelp n = it.next();
			if (Validator.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<PlatformHelp> list, PlatformHelp t) {
		return getChildList(list, t).size() > 0;
	}
}
